package com.qixin.part.controller;

import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.*;
import com.qixin.api.service.ComCarService;
import com.qixin.api.service.ComFileService;
import com.qixin.api.service.ComIdcardFileService;
import com.qixin.util.file.upload.UploadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/car")
public class CarController {

    private static final Logger logger = LoggerFactory.getLogger(CarController.class);
    @Autowired
    ComFileService comFileService;
    @Autowired
    ComCarService  comCarService;

    /**
     * 跳转页面车源加盟
     * @return
     */
/*    @RequestMapping("/main.html")
    public String carftl(){
        return "company/car_join";

    }*/
    /**
     * 跳转页面车源加盟上传认证
     * @return
     */
    @RequestMapping("/add.html")
    public String add(){
        return "company/car_join_add";

    }
    /**
     * 跳转页面租车详情
     * @return
     */
    @RequestMapping("/car_rental.html")
    public String carrental(){
        return "company/car_rental";}
    /**
     * 账单管理
     * @return
     */
    @RequestMapping("/bill.html")
    public String bill(){
        return "company/Bill";
    }
    /**
     * 跳转页面租车详情添加成员
     * @return
     */
    @RequestMapping("/car_rental_add.html")
    public String car_rental_add(){
        return "company/car_rental_add";

    }

    /**
     * 文件上传
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public FileResult upload(@RequestParam("file") MultipartFile file,@RequestParam("isCarIndex") Integer isCarIndex) {
        ComFile comFile=new ComFile();
        comFile.setIsCarIndex(isCarIndex);
        return  comFileService.upload(file,comFile);
    }
    /**
     * 车源加盟列表查询
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultPage<ComCar> list(VoPage voPage,HttpSession session) {
        ResultPage<ComCar> resultPage= comCarService.selectMyPage(voPage,session );
        return  resultPage;
    }
	/**
	 * 租车详情列表查询
	 * @return
	 */
	@RequestMapping("/carlist")
	@ResponseBody
	public ResultPage<ComCar> carlist(VoPage voPage, HttpSession session,Long id) {
        ResultPage<ComCar> resultPage= comCarService.selectaLL(voPage,session,id);
		return  resultPage;
	}
    /**
     *
     * 车源搜索
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public ResultPage<ComCar> search(VoPage voPage,  Long startTime, Long endTime) {

        Map map=new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        ResultPage<ComCar> resultPage= comCarService.search(map,voPage);
        return  resultPage;
    }
    /**
     * 车源加盟添加
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String param,Long[] filesId) {
        logger.info(param);
        ComCar comCar=JSONObject.parseObject(param,ComCar.class);
        comCar.setUpcarTime(new Date().getTime());
        comCar.setIsShow(2);
        try {
            return    comCarService.insertCar(comCar,filesId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "保存出现意外，有图片丢失。";
        }

    }
    /*
    批量删除
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public  void deleteAll(@RequestParam("ids[]")String[] ids){
        Map map = new HashMap<>();
        int i;
        for(i=0;i< ids.length;i++){
            map.put("id",ids[i]);
            comCarService.deleteByMap(map);

        }

    }
    /*
    跳转页面车辆明细
     */
    @RequestMapping("/cardetail.html")
    public String cardetail(Model model,HttpSession session){
		String ptname=((ComPtUser)session.getAttribute("user")).getPtname();
        model.addAttribute("ptname",ptname);
        return "company/cardetail";
    }
    /*
跳转页面车辆租出
 */
    @RequestMapping("/carrent.html")
    public String carrent( Model model,String ptname){
        model.addAttribute("ptname",ptname);
        return "company/carrent";
    }
    /*
跳转页面车辆租入
 */
    @RequestMapping("/carget.html")
    public String carget(Model model,String ptname){
        model.addAttribute("ptname",ptname);
        return "company/carget";

    }
    /**
     * 车辆明细列表查询
     * @return
     */
    @RequestMapping("/carclist")
    @ResponseBody
    public ResultPage<ComCar> carclist(VoPage voPage,String carType,HttpSession session,Long id) {

        ResultPage<ComCar> resultPage= comCarService.selectMyPage2(voPage,carType, session,id);
        return  resultPage;
    }
    /**
     * 账单管理查询
     * @return
     */
    @RequestMapping("/billlist")
    @ResponseBody
    public ResultPage<ComCar> billlist(VoPage voPage,HttpSession session) {
        ResultPage<ComCar> resultPage= comCarService.selectBill(voPage,session);
        return  resultPage;
    }
    /**
     * 车辆明细搜索
     * @return
     */
    @RequestMapping("/searchcardetail")
    @ResponseBody
    public ResultPage<ComCar> searchcardetail(VoPage voPage, String carBrand,String ptname) {

        Map map=new HashMap();
        map.put("carBrand",carBrand);
        map.put("ptname",ptname);
        ResultPage<ComCar> resultPage= comCarService.searchcardetail(map,voPage);
        return  resultPage;
    }
    //已租出变未租出
    @RequestMapping("/update")
    @ResponseBody
    public String update(Long id){
        logger.info("修改");
        return comCarService.updatesentsu(id);

    }

    //未租出变已租出
    @RequestMapping("/update2")
    @ResponseBody
    public String update2(Long id){
        logger.info("未租出变已租出");
        return comCarService.updatesentsu2(id);
    }
    //已租入变未租入
    @RequestMapping("/update3")
    @ResponseBody
    public String update3(Long id){
        logger.info("修改");
        return comCarService.updatesentsu3(id);
    }
    //未租入变已租入
    @RequestMapping("/update4")
    @ResponseBody
    public String update4(Long id){
        logger.info("修改");
        return comCarService.updatesentsu4(id);
    }
    //获取图片
    @RequestMapping("/getPhoto")
    public   void getPhoto(HttpServletResponse response,@RequestParam("path") String path) throws Exception {
        System.out.println("path= "+path);
        File file = new File(path);
        FileInputStream fis; fis = new FileInputStream(file);
        long size = file.length();
        byte[] temp = new byte[(int) size];
        fis.read(temp, 0, (int) size);
        fis.close();
        byte[] data = temp;
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        out.write(data);
        out.flush();
        out.close();
    }


    /**
     * 跳转已租车辆
     * @return
     */
    @RequestMapping("/carGo.html")
    public String carGo(){
        return "/company/cargo";
    }





}
